﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Infrastructure.Core.IOC;
using SMG.ServiceContract.Program;

namespace ManageForm
{
    public partial class ProgramForm : Form
    {
        //节目单服务
        IProgramBillService billService = ServiceLocator.Instance.Resolve<IProgramBillService>();
        //频道服务
        IChannelService channelService = ServiceLocator.Instance.Resolve<IChannelService>();

        public ProgramForm()
        {
            InitializeComponent();
        }

        #region Control Events

        private void BillListForm_Load(object sender, EventArgs e)
        {
            InitFormData();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            SetBillTemplateForm form = new SetBillTemplateForm();
            form.ShowDialog();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            BillResourcesForm form = new BillResourcesForm();
            form.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.allenPage1.PageIndex = 1;
            this.allenPage1.RecordCount = 0;
            this.dgBillView.DataSource = null;
            var versionId = this.cbBillVersion.SelectedValue;
            LoadBillList(versionId == null ? String.Empty : versionId.ToString());
        }

        private void dtkPlayDate_ValueChanged(object sender, EventArgs e)
        {
            LoadBillVersionList();
        }

        private void cbChannel_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadBillVersionList();
        }

        private void allenPage1_PageIndexChanged(object sender, EventArgs e)
        {
            var versionId = this.cbBillVersion.SelectedValue;
            LoadBillList(versionId == null ? String.Empty : versionId.ToString());
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        void InitFormData() {
            //InitQueryCondition();
        }
        /// <summary>
        /// 初始化查询条件
        /// </summary>
        void InitQueryCondition() {
            var channels = channelService.QueryChannelAll();
            this.cbChannel.DataSource = channels;
            this.cbChannel.ValueMember = "ChannelId";
            this.cbChannel.DisplayMember = "ChannelName";
            this.dgBillView.AutoGenerateColumns = false;
            LoadBillVersionList();
        }
        /// <summary>
        /// 加载节目单版本列表信息
        /// </summary>
        void LoadBillVersionList() {
            var channelId = this.cbChannel.SelectedValue.ToString();
            var playDate = this.dtkPlayDate.Value.ToString("yyyy-MM-dd");

            //查询节目单版本列表
            var billVersions = billService.QueryBillVersion(channelId, playDate);
            this.cbBillVersion.DataSource = billVersions;
            this.cbBillVersion.ValueMember = "ID";
            this.cbBillVersion.DisplayMember = "BillName";
        }
        /// <summary>
        /// 绑定节目单列表信息
        /// </summary>
        void LoadBillList(String versionId) {
            this.dgBillView.DataSource = null;
            long vid;
            if (long.TryParse(versionId, out vid)) {
                var pageSize = this.allenPage1.PageSize;
                var pageIndex = this.allenPage1.PageIndex;
                var bills = billService.QueryBillByVersion(vid, pageIndex, pageSize);
                this.allenPage1.RecordCount = bills.TotalRecords;
                this.dgBillView.DataSource = bills.Data;
            }
        }

        #endregion

    }
}
